package stud.mpgame.old.game;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import stud.mpgame.network.Message;
import stud.mpgame.network.UserObject;
import stud.mpgame.snapshot.ICommand;
import stud.mpgame.snapshot.ISnapshot;

public class GameCommand extends UserObject implements ICommand {

    private int keyCode;
    private boolean keyStatus;
    private IoSession session;
    private int time;

    private static String name[] = {"Up", "Right", "Down", "Left"};
    // UP_KEY_CODE = 0;
    // RIGHT_KEY_CODE = 1;
    // DOWN_KEY_CODE = 2;
    // LEFT_KEY_CODE = 3;
    // KEY_DOWN = 1;
    // KEY_UP = 0;

    public GameCommand() {}

    @Override
    public void readExternal(IoBuffer buffer, Message owner) {
        this.keyCode =  buffer.getInt();
        this.keyStatus = (buffer.getInt() > 0);
    }

    public int getKeyCode() {
        return keyCode;
    }

    public boolean getKeyStatus() {
        return keyStatus;
    }

    public IoSession getSession() {
        return session;
    }

    public int getTime() {
        return time;
    }

    public void setSession(IoSession session) {
        this.session = session;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public String toString() {
        String result = name[keyCode] + " key ";
        result += (keyStatus ? "pressed" : "released");
        return result;
    }

    public void apply(ISnapshot snapshot) {
        ((GameWorld) snapshot).applyGameCommand(session, this);
    }

}

